﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Media;
using WDCF;
using Condition = WDCF.Condition;

namespace WPFDataGridFormattingTestUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.dataGrid.ItemsSource = GetTestProducts(20);

            //var table = GetTestTable(20);

            //this.dataGrid.ItemsSource = table.DefaultView;
            
            //var conditions = new List<Condition>
            //                     {
            //                         new Condition("Price", NumericOperator.GreaterThan, "50")
            //                     };

            //new DataGridConditionalRowFormatter(this.dataGrid, conditions)
            //{
            //    ConditionalBackgroundColor = new LinearGradientBrush(
            //        new GradientStopCollection(new List<GradientStop> 
            //        {
            //            new GradientStop(Colors.White,0),
            //            new GradientStop(Colors.Gray,0.8),
            //            new GradientStop(Colors.DarkGray,1.0)
            //        }),
            //        90),
            //    ConditionalTextColor = Brushes.White
            //};

            new DataGridConditionalCellFormatter(this.dataGrid, new Condition("InStock"))
            {
                ConditionalBackgroundColor = Brushes.Orange,
                NormalBackgroundColor = Brushes.Transparent
            };
        }

        public IEnumerable<Product> GetTestProducts(int amount)
        {
            IList<Product> result = new List<Product>();
            var random = new Random();
            for (int i = 1; i <= amount; i++)
            {
                var product = new Product
                                  {
                                      Name = "Product" + i,
                                      ProductNumber = i + "" + i*2 + "" + i + "" + i*i + "" + i,
                                      Price = random.Next(5, 300)*0.5m,
                                      InStock = random.Next(2, 10)%2 == 0 ? true : false
                                  };

                result.Add(product);
            }

            return result;
        }

        public IEnumerable<Product> GetTestProduct()
        {
            IList<Product> result = new List<Product>();
            var product = new Product
                              {
                                  Name = "Product0",
                                  ProductNumber = "12345678",
                                  Price = 52,
                                  InStock = false
            };
            result.Add(product);
            return result;
        }

        public DataTable GetTestTable(int entries)
        {
            var table = new DataTable();
            table.Columns.Add("Name", typeof (String));
            table.Columns.Add("ProductNumber", typeof(String));
            table.Columns.Add("Price", typeof(Decimal));
            table.Columns.Add("InStock", typeof(bool));

            var random = new Random();
            for (int i = 0; i < entries; i++)
            {
                table.Rows.Add(new object[]
                                   {
                                       "Product" + i, i + "" + i*2 + "" + i + "" + i*i + "" + i, random.Next(5, 300)*0.5m,
                                       random.Next(2, 10)%2 == 0 ? true : false
                                   });
            }

            return table;
        }

        public DataSet GetTestDataSet()
        {
            var set = new DataSet();
            set.Tables.Add(GetTestTable(20));
            set.Tables.Add(GetTestTable(15));

            return set;
        }
    }

    public class Product
    {
        public string Name { get; set; }
        public string ProductNumber { get; set; }
        public decimal Price { get; set; }
        public bool InStock { get; set; }
    }
}
